package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.Professor;
import cn.bx.bid.entity.WorkHistory;
import cn.bx.bid.service.ProfessorService;
import cn.bx.bid.util.DocUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
//@RequestMapping("/user")
//控制器
public class ProfessorController {
    //添加日志
    private static final Logger Log = LoggerFactory.getLogger(ProfessorController.class);

    @Autowired
    private ProfessorService professorService;

    //produces = "application/json;charset=UTF-8"   意思返回值强转成JSON
    @RequestMapping(value = "professorGet", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Professor professorGet(@RequestParam("id") long id) {
        Professor professor = this.professorService.get(id);
        System.out.println("*****************" + professor);
        return professor;
    }

    @RequestMapping(value = "professorUpdate", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public int professorUpdate(@ModelAttribute("p") Professor p) {
        int update = this.professorService.update(p);
        return update;
    }

    //    historyGet 通过prifessorid
    @RequestMapping(value = "historyGet", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public WorkHistory historyGet(@RequestParam("id") long id) {
        WorkHistory history = this.professorService.getHistory(id);
        System.out.println("*****************" + history);
        return history;
    }

    //historyGetid 工作历史更改同过id
    @RequestMapping(value = "historyGetid", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public WorkHistory historyGetid(@RequestParam("id") long id) {
        WorkHistory history = this.professorService.getHistoryid(id);
        System.out.println("*****************" + history);
        return history;
    }

    //    //    historyUpdate
    @RequestMapping(value = "historyUpdate", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public int historyUpdate(@ModelAttribute("p") WorkHistory p) {
        int update = this.professorService.historyUpdate(p);
        return update;
    }

    @RequestMapping(value = "workhistoryDel", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public int workhistoryDel(@RequestParam("id") long id) {
        int delHis = this.professorService.delHis(id);
        return delHis;
    }

    //
////    historyAdd
    @RequestMapping("historyAdd")
    @ResponseBody
    public int historyAdd(@ModelAttribute("p") WorkHistory p) {
        int addHistory = this.professorService.addHistory(p);
        System.out.println(addHistory);
        return addHistory;

    }

    @RequestMapping(value = "professorSearch")
    @ResponseBody
    public Object paging(@RequestParam(value = "page", defaultValue = "1") int pageNo, @RequestParam(value = "limit", defaultValue = "10") int limit, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "tele", required = false) String tele, @RequestParam(value = "identityNumber", required = false) String identityNumber) {
//        ModelAndView mv=new ModelAndView();
//        mv.addObject("name",name);
//        mv.addObject("pageNo",pageNo);
//        mv.addObject("limit",limit);
//        mv.addObject("professor",this.professorService.paging(pageNo,limit,name,tele,identityNumber));
//      //  mv.addObject("p2",this.userService.paging(name,start,end));
//        mv.setViewName("professorlist");
        Page<Professor> page = this.professorService.paging(pageNo, limit, name, tele, identityNumber);
        return page;
    }

    //u还在上下文中，叫u
    @RequestMapping("professorAdd")
    @ResponseBody
    public int professorAdd(@ModelAttribute("p") Professor p) {
        int add = this.professorService.add(p);
        return add;
    }


    //    @RequestMapping("professorDel-{id}")
//    public  String   userDel(@PathVariable("id") long[] ids, RedirectAttributes rd){
//        int i = this.professorService.batchDel(ids);
//        rd.addFlashAttribute("professorDelmsg",i>0?"删除成功":"删除失败");
//        return "redirect:/userSearch.jhtml";
//    }
    @RequestMapping("batchDelProfessor")
    @ResponseBody
    public int batchDelProfessor(@RequestParam("ids[]") long[] ids) {
        int batchDel = this.professorService.batchDel(ids);
        return batchDel;
    }

    //    historyGet 通过prifessorid
  /*  @RequestMapping(value = "historyGet", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public WorkHistory historyGet(@RequestParam("id") long id) {
        WorkHistory history = this.professorService.getHistory(id);
        System.out.println("*****************" + history);
        return history;
    }*/

    //查找专家参与过的项目
    @RequestMapping(value = "getproject", produces = "application/json;charset=UTF-8")
    @ResponseBody
    public Object getproject(@RequestParam("id") long id) {
        Professor w = this.professorService.projectGet(id);
        return w;
    }


    @RequestMapping("exportProToWord")
    @ResponseBody
    public void exportProToWord(@RequestParam("id") long id, HttpServletRequest request, HttpServletResponse response) throws Exception {
        Professor p = this.professorService.get(id);
        // System.out.println(p.getProfessorName()+"*****************************************************");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("p", p);
        String path = Math.random() + ".doc";
        DocUtil.exportProfessor(path, map);
        File file = new File(path);
        response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(p.getProfessorName() + ".doc", "UTF-8"));
        FileInputStream fin = new FileInputStream(file);
        byte[] b = new byte[fin.available()];
        fin.read(b);
        fin.close();
        ServletOutputStream out = response.getOutputStream();
        out.write(b);
        out.flush();
        file.delete();//下载后删除文件
    }

    @InitBinder
    public void init(WebDataBinder binder) {
        CustomDateEditor dateEditor = new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true);
        binder.registerCustomEditor(Date.class, dateEditor);
    }
}
